<%--
  Created by IntelliJ IDEA.
  User: w1057229265
  Date: 2021/5/25
  Time: 9:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="../static/plugins/jquery-easyui-1.9.15/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="../static/plugins/jquery-easyui-1.9.15/themes/icon.css">
    <script type="text/javascript" src="../static/plugins/jquery-easyui-1.9.15/jquery.min.js"></script>
    <script type="text/javascript" src="../static/plugins/jquery-easyui-1.9.15/jquery.easyui.min.js"></script>
   <script>
       $(function () {
           loadData('');

           $('#btnAdd').click(function () {
               $('#dd').dialog({
                   title: '添加图书',
                   href:'goodsform.jsp',
                   width: 400,
                   height: 300,
                   closed: false,
                   cache: false,
                   modal: true,
                   buttons:[{
                       text:'保存',
                       iconCls:'icon-save',
                       handler:function (){
                           $.messager.progress();	// display the progress bar
                           $('#ff').form('submit', {
                               url: '/ServletBo?action=reg',
                               onSubmit: function(){
                                   var isValid = $(this).form('validate');
                                   if (!isValid){
                                       $.messager.progress('close');	// hide progress bar while the form is invalid
                                   }
                                   return isValid;	// return false will stop the form submission
                               },
                               success: function(data){
                                   var data = eval('(' + data + ')'); // change the JSON string to javascript object
                                   if (data.code == '200'){
                                       $.messager.alert('提示',data.msg + '图书添加成功', 'info')
                                       location.href="goodslist.jsp"
                                   }
                                   else {
                                       $.messager.alert('提示',data.msg , 'warning')
                                   }
                                   $.messager.progress('close');	// hide progress bar while submit successfully
                               }
                           });
                       }
                   },
                       {
                           text: '退出',
                           iconCls:'icon-back',
                           handler:function (){
                               location.href='/goodslist.jsp';
                           }
                       }]
               });
           })
           $('#btnDelete').click(function () {
               $.messager.confirm('提示', '是否要删除该数据？', function (r) {
                   if (r) {
                       let row = $('#dg').datagrid("getSelected");
                       $.ajax({
                           type: "GET",
                           url: "/ServletBo?action=delete",
                           data: {bid: row.bid},
                           success: function (ret) {
                               let result = eval("(" + ret + ")");
                               if (result.code == "200") {
                                   $.messager.show({
                                       title: '提示',
                                       msg: result.msg,
                                       showType: 'show',
                                       timeout: 2000,
                                       showType: 'slide'
                                   });
                                   $('#dg').datagrid('reload');
                               } else {
                                   $.messager.alert('提示', result.msg, 'warning');

                               }

                           }
                       })
                   }
               })
           })
           $('#btnEdit').click(function () {
               if ($('#dg').datagrid('getSelected') == null) {
                   $.messager.alert("提示", "请选择要编辑的数据", 'warning')
                   return;
               }
               $('#dd').dialog({
                   title: '用户修改中心',
                   width: 400,
                   height: 300,
                   href:'goodsform.jsp?action=edit',
                   closed: false,
                   cache: false,
                   modal: true,
                   buttons:[{
                       text:'保存',
                       iconCls:'icon-save',
                       handler: function () {
                           let isOK = $('#form').form('validate');
                           if (isOK) {
                               $.messager.confirm('确认', '是否确认修改', function (r) {
                                   if (r) {
                                       SaveData('update');
                                   }
                               });
                           }

                       }
                   }, {
                       text: '退出',
                       iconCls: 'icon-back',
                       handler: function () {

                       }
                   }]
               });
           })
           $('#BtnQuery').textbox({
               onClickButton:function (){
                   alert($(this).val())
                   loadData($(this).val())
               }
           })
           function SaveData(action) {
               $.messager.progress();	// display the progress bar
               $('#ff').form('submit', {
                   url: '/ServletBo?action=' + action,
                   onSubmit: function () {
                       var isValid = $(this).form('validate');
                       if (!isValid) {
                           $.messager.progress('close');	// hide progress bar while the form is invalid
                       } else {

                       }
                       return isValid;	// return false will stop the form submission
                   },
                   success: function (data) {
                       let result = eval("(" + data + ")");
                       dataType:'json'
                       if (result.code == '200') {
                           $.messager.alert('提示', result.msg, 'info');
                           $('#dd').dialog('close');
                           $('#dg').datagrid('reload');
                       } else
                           $.messager.alert('提示', result.msg, 'warning');
                       $.messager.progress('close');

                   }
               });
           }
           function  loadData(val){

               $('#dg').datagrid({
                   url: '/ServletBo?action=getdglist',
                   toolbar: '#tb',
                   pagination: true,
                   fit: true,
                   singleSelect: true,
                   queryParams: {
                       bname: val
                   },
                   columns: [[
                       {field: 'bid', title: '书码', width: 100},
                       {field: 'bname', title: '书名称', width: 100},
                       {field: 'bprice', title: '价格', width: 100},
                       {field: 'bimage', title: '仓库', width: 300},
                       {field: 'bargainer', title: '特价', width: 100},
                       {field: 'isHot', title: '最新', width: 100},
                       {field: 'bpdesc', title: '介绍', width: 100},
                       {field: 'cid', title: '类型'}
                   ]]
               });
           }

       })
   </script>
</head>
<body class="easyui-layout">
<div data-options="region:'center',border:false">
    <table id="dg">

    </table>
</div>
<div id="dd"></div>
<div id="tb" style="height:auto">
    <table>
        <tr>
            <td><a id="btnAdd" href="javascript:void(0)" class="easyui-linkbutton"
                   data-options="iconCls:'icon-add',plain:true">添加</a></td>
            <td>
                <div class="datagrid-btn-separator"></div>
            </td>
            <td><a id="btnEdit" href="javascript:void(0)" class="easyui-linkbutton"
                   data-options="iconCls:'icon-edit',plain:true">编辑</a></td>
            <td>
                <div class="datagrid-btn-separator"></div>
            </td>
            <td><a id="btnDelete" href="javascript:void(0)" class="easyui-linkbutton"
                   data-options="iconCls:'icon-remove',plain:true">删除</a>
            </td>
            <td>
                <input id="BtnQuery" class="easyui-textbox" data-options="buttonText:'查询',buttonAlign:'right',buttonIcon:'icon-search',prompt:'请输入'" style="width:280px;">
            </td>
        </tr>
    </table>
</div>
</body>
</html>
